package com.youkeda.meetingroom.dao.mapper;

import com.youkeda.meetingroom.dao.dataobject.MrUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper

public interface MrUserMapper {

    @Delete({
            "delete from mr_user",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String id);


    @Insert({
            "insert into mr_user (id, name, ",
            "company_id, position, ",
            "is_leader, gmt_created, ",
            "gmt_modified, email, ",
            "pwd, salt)",
            "values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, ",
            "#{companyId,jdbcType=VARCHAR}, #{position,jdbcType=VARCHAR}, ",
            "#{isLeader,jdbcType=VARCHAR}, #{gmtCreated,jdbcType=TIMESTAMP}, ",
            "#{gmtModified,jdbcType=TIMESTAMP}, #{email,jdbcType=VARCHAR}, ",
            "#{pwd,jdbcType=VARCHAR}, #{salt,jdbcType=VARCHAR})"
    })
    int insert(MrUser record);


    @Select({
            "select",
            "id, name, company_id, position, is_leader, gmt_created, gmt_modified, email, ",
            "pwd, salt",
            "from mr_user",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_id", property = "companyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "position", property = "position", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_leader", property = "isLeader", jdbcType = JdbcType.VARCHAR),
            @Result(column = "gmt_created", property = "gmtCreated", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "gmt_modified", property = "gmtModified", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "email", property = "email", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pwd", property = "pwd", jdbcType = JdbcType.VARCHAR),
            @Result(column = "salt", property = "salt", jdbcType = JdbcType.VARCHAR)
    })
    MrUser selectByPrimaryKey(String id);


    @Update({
            "update mr_user",
            "set name = #{name,jdbcType=VARCHAR},",
            "company_id = #{companyId,jdbcType=VARCHAR},",
            "position = #{position,jdbcType=VARCHAR},",
            "is_leader = #{isLeader,jdbcType=VARCHAR},",
            "gmt_created = #{gmtCreated,jdbcType=TIMESTAMP},",
            "gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},",
            "email = #{email,jdbcType=VARCHAR},",
            "pwd = #{pwd,jdbcType=VARCHAR},",
            "salt = #{salt,jdbcType=VARCHAR}",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(MrUser record);


    /**
     * 通过email(用户邮箱)查询用户信息(登录)
     * @param email 用户邮箱
     * @return 用户信息
     */

    @Select({
            "select",
            "id, name, pwd,salt",
            "from mr_user",
            "where email = #{email,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pwd", property = "pwd", jdbcType = JdbcType.VARCHAR),
            @Result(column = "salt", property = "salt", jdbcType = JdbcType.VARCHAR)
    })
    MrUser selectUserByEmail(String email);

    /**
     * 通过用户id(主键)查询公司id
     *
     * @param id 用户id(主键)
     * @return 公司id
     */
    @Select({
            "select",
            "company_id",
            "from mr_user",
            "where id = #{id,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "company_id", property = "companyId", jdbcType = JdbcType.VARCHAR),
    })
    MrUser selectComidByPrimaryKey(String id);
}